package com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system;

import com.glsc.ngateway.common.api.platform.dto.system.GwContactDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.GwContact;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GwContractRepository extends JpaRepository<GwContact, Integer>, JpaSpecificationExecutor<GwContact> {

    @Query(value = "select distinct new GwContact(m.id,m.contactType,m.contactPerson, m.contactPhone, m.contactMobile, m.contactEmail, m.deleted, v.id, v.vendorName)  from GwContact m " +
            "left join SystemVendorContractRelation r on m.id = r.contactId and r.contactType = '1' " +
            "left join SystemVendorInfo v on v.id = r.vendorId " +
            "where 1=1 " +
//            "and (:#{#paramSearchDto.vendorName} is null OR v.vendorName = :#{#paramSearchDto.vendorName})  " +
            "and (:#{#paramSearchDto.vendorId} is null OR r.vendorId = :#{#paramSearchDto.vendorId})  " +
            "and (:#{#paramSearchDto.contactPerson} is null OR m.contactPerson = :#{#paramSearchDto.contactPerson})  " +
            "and (:#{#paramSearchDto.contactMobile} is null OR m.contactMobile LIKE CONCAT('%',:#{#paramSearchDto.contactMobile},'%') )  " +
//            "and (:#{#paramSearchDto.processDefineKey} is null OR m.processDefineKey = :#{#paramSearchDto.processDefineKey} )  " +
//            "and (:#{#paramSearchDto.systemCode} is null OR m.systemCode = :#{#paramSearchDto.systemCode} )  " +
//            "and (:#{#paramSearchDto.beginDateLocalDate} is null OR m.processCreateTime >= :#{#paramSearchDto.beginDateLocalDate} )  " +
//            "and (:#{#paramSearchDto.endDateLocalDate} is null OR m.processCreateTime <= :#{#paramSearchDto.endDateLocalDate} )  " +
//            "and (:#{#paramSearchDto.hasProcessEnd} is null OR m.isClose = :#{#paramSearchDto.hasProcessEnd} )  " +
//            "and (coalesce(:#{#paramSearchDto.adminList}, null) is null or m.systemAdminAccount in (:#{#paramSearchDto.adminList})) " +
            "and (:#{#paramSearchDto.deleted} is null OR m.deleted = :#{#paramSearchDto.deleted} )  " +
            "")
    Page<GwContact> searchByParam(Pageable pageable, GwContactDto paramSearchDto);

    List<GwContact> findAllByIdIn(List<Integer> idList);

    @Query(value="select distinct new GwContact(m.id,m.contactType,m.contactPerson, m.contactPhone, m.contactMobile, m.contactEmail, m.deleted, v.id, v.vendorName)  from GwContact m " +
            "left join SystemVendorContractRelation r on m.id = r.contactId and r.contactType = '1' " +
            "left join SystemVendorInfo v on v.id = r.vendorId " +
            "where m.id=:#{#id} ")
    GwContact queryById(Integer id);
}